package com.tripupdater.command
{
	import com.facebook.Facebook;
	import com.facebook.commands.friends.AreFriends;
	import com.facebook.commands.users.GetInfo;
	import com.facebook.data.auth.GetSessionData;
	import com.facebook.data.friends.AreFriendsData;
	import com.facebook.data.friends.FriendsData;
	import com.facebook.data.users.FacebookUser;
	import com.facebook.data.users.GetInfoData;
	import com.facebook.data.users.GetInfoFieldValues;
	import com.facebook.events.FacebookEvent;
	import com.facebook.net.FacebookCall;
	import com.facebook.utils.FacebookConnectUtil;
	import com.facebook.utils.FacebookSessionUtil;
	import com.tripupdater.model.Model;
	import com.tripupdater.view.TripUpdaterMainView;
	import com.turner.caf.commands.ICommand;
	import com.turner.caf.control.CAFEvent;
	
	import flash.events.TimerEvent;
	import flash.utils.Timer;

	public class GetFacebookCommand implements ICommand
	{
		private var API_KEY:String;
		private var SECRET_KEY:String;
		private var USER_ID:String;
		
		private var fb:Facebook;
		private var fbSession:FacebookSessionUtil;
		private var fbCall:FacebookCall;
		private var fbConnect:FacebookConnectUtil;

		private var target:TripUpdaterMainView;
		private var timer:Timer;
		private var model:Model;
		
		public function GetFacebookCommand()
		{
			API_KEY= "649c787cfd8a0d5a3640884d1ed48285";
			SECRET_KEY= "8608160356b93cddd1bf6ef7c8834a61";
			USER_ID= "1012907369";
			
			model= Model.getInstance();
		}

		public function execute(event:CAFEvent):void
		{
			target= event.viewTarget as TripUpdaterMainView;

			timer= new Timer(1000);
			timer.addEventListener(TimerEvent.TIMER, onTimer);
			timer.start();
			
			fbSession= new FacebookSessionUtil(API_KEY, SECRET_KEY, target.loaderInfo);
			fb= fbSession.facebook;
			
			fbSession.addEventListener(FacebookEvent.CONNECT, onConnect);
			fbSession.login();
		}
		
		private function onTimer(e:TimerEvent):void
		{
			fbSession.validateLogin();
		}
		
		private function onConnect(e:FacebookEvent):void
		{
			if(e.data is GetSessionData){
				timer.removeEventListener(TimerEvent.TIMER, onTimer);
				timer.stop();
				if(fb.uid != USER_ID){
					fbCall= fb.post(new AreFriends([fb.uid], [USER_ID]));
					fbCall.addEventListener(FacebookEvent.COMPLETE, onAreFriends);
				} else {
					fbCall= fb.post(new GetInfo([USER_ID], [GetInfoFieldValues.ALL_VALUES]));
					fbCall.addEventListener(FacebookEvent.COMPLETE, onGetInfo);
				}
			}
		}
		
		private function onAreFriends(e:FacebookEvent):void
		{
			var areFriends:FriendsData= (e.data as AreFriendsData).friendsCollection.getItemAt(0) as FriendsData;
			if(areFriends.are_friends){
				fbCall= fb.post(new GetInfo([USER_ID], [GetInfoFieldValues.ALL_VALUES]));
				fbCall.addEventListener(FacebookEvent.COMPLETE, onGetInfo);
			} else {
				model.facebookData= null;
				
				target.onFacebookResult();
			}
		}
		
		private function onGetInfo(e:FacebookEvent):void
		{
			model.facebookData= (e.data as GetInfoData).userCollection.getItemAt(0) as FacebookUser;
			
			target.onFacebookResult();
		}
	}
}